<?php

class lib_arraytree
{
    private $__data = array();
    
    /**
    * put your comment there...
    * 
    * @param mixed $data
    * @return lib_arraytree
    */
    public function lib_arraytree($data)
    {
        $this->__data = $data; 
        
    }
    
    public function restart($data)
    {
        $this->__data = $data;
    }
    
    public function add($row , $parent_id = 0)
    {
        if($parent_id>0 && !isset($this->__data[$parent_id]))
            return false;
        
        //key 从 1开始 
        if(count($this->__data) > 0)
        {
            $this->__data[] = array(
                'parent_id' => $parent_id,
                'row' => $row
            );
        }
        else 
        {
            $this->__data[1] = array(
                'parent_id' => $parent_id,
                'row' => $row
            );
        }
        
        return true;
    }
    
    public function edit($id , $row , $parent_id = 0)
    {
        $this->__data[$id]['parent_id'] = $parent_id;
        $this->__data[$id]['row'] = $row;
        return true;
    }
    
    public function del($id)
    {
        unset($this->__data[$id]);
        return true;
    }
    
    public function get_id2field($field , $sub_suffix = '')
    {
        $aList = array();
        if(count($this->__data) > 0)
        {
            foreach($this->__data as $k => $row)
            {
                if($row['parent_id'] == 0)
                {
                    $key = str_pad($k , 2 , 0 , STR_PAD_LEFT).'0000';
                    $aList[$key] = array('id'=>$k , 'v'=>$row['row'][$field]);
                }
                else 
                {
                    $parent_id = str_pad($row['parent_id'] , 2 , 0 , STR_PAD_LEFT);
                    $key = str_pad($k , 2 , 0 , STR_PAD_LEFT);
                    $aList[$parent_id.'00'.$key] = array('id'=>$k , 'v'=>$sub_suffix.$row['row'][$field]);
                }
            }
            
            
            ksort($aList);
            reset($aList);
            
            $aRs = array();
            foreach ($aList as $row)
            {
                $aRs[$row['id']] = $row['v'];
            }
            
        }
        
        return $aRs;
    }
    
    public function get_id2field_meta($field = null)
    {
        $aList = array();
        if(count($this->__data) > 0)
        {
            foreach ($this->__data as $id => $row)
            {
                $aList[$id] = is_null($field) ? $row['row'] : $row['row'][$field];
            }
        }
        return $aList;
    }
    
    public function get_list()
    {
        $aList = array();
        if(count($this->__data) > 0)
        {
            foreach($this->__data as $k => $row)
            {
                if($row['parent_id'] == 0)
                {
                    $aList[$k] = array(
                        'id' => $k,
                        'row' => $row['row']
                    );
                }
                else 
                {
                    $aList[$row['parent_id']]['sub'][$k] = array(
                        'id' => $k,
                        'row' => $row['row']
                    );
                }
                
            }
            
        }
        
        return $aList;
    }
    
    public function get_parents()
    {
        $aList = array();
        foreach ($this->__data as $k=>$row)
        {
            if($row['parent_id'] == 0)
                $aList[$k] = $row['row'];
        }
        return $aList;
    }
    public function get_children_id($parent_id)
    {
        $aList = array();
        foreach ($this->__data as $k=>$row)
        {
            if($row['parent_id'] == $parent_id)
                $aList[$k] = $k;
        }
        return $aList;
    }
    
    public function get_by_id($id)
    {
        return $this->__data[$id];
    }
    
    public function dump()
    {
        return $this->__data;
    }
}

?>
